package com.yaowk.weixin.controller;

import com.feizhou.swagger.annotation.Api;
import com.feizhou.swagger.annotation.ApiOperation;
import com.feizhou.swagger.annotation.Param;
import com.feizhou.swagger.annotation.Params;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.yaowk.common.controller.BaseController;
import com.yaowk.system.validator.PlatformIdValidator;
import com.yaowk.weixin.model.Reply;
import com.yaowk.weixin.model.ReplyInfo;
import com.yaowk.weixin.model.ReplyRelate;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yaowk on 17-8-1.
 */
@Before(PlatformIdValidator.class)
@Api(tag = "replyInfo", description = "自动回复数据", url = "/admin/weixin/replyInfo")
@RequiresAuthentication
@RequiresPermissions("weixin:replyInfo")
public class ReplyInfoController extends BaseController {

    @ApiOperation(description = "查询列表", url = "/list")
    @Params({
            @Param(name = "platformId", description = "平台id", required = true),
            @Param(name = "pageSize", description = "分页大小", defaultValue = "20"),
            @Param(name = "pageNumber", description = "当前页", defaultValue = "1")
    })
    public void list() {
        Integer platformId = getParaToInt("platformId");
        Page<ReplyInfo> page = ReplyInfo.dao.paginate(getPage(), Kv.by("platform_id = ", platformId));
        renderJson(page);
    }

    @ApiOperation(description = "回复内容详情", url = "/{replyInfoId}")
    @Params({
            @Param(name = "platformId", description = "平台id", required = true)
    })
    public void index() {
        Integer id = getParaToInt();
        ReplyInfo replyInfo = ReplyInfo.dao.findById(id);
        if (ObjectUtil.isNotNull(replyInfo)) {
            List<Reply> replies = new ArrayList<>();
            replyInfo.put("replyList", replies);
            List<ReplyRelate> replyRelates = ReplyRelate.dao.find(Kv.by("info_id = ", id));
            for (ReplyRelate replyRelate : replyRelates) {
                Reply reply = Reply.dao.findById(replyRelate.getReplyId());
                if (ObjectUtil.isNotNull(reply)) {
                    replies.add(reply);
                }
            }
        }
        renderJson(replyInfo);
    }

    @ApiOperation(description = "添加回复内容", url = "/add")
    @Params({
            @Param(name = "platformId", description = "平台id", required = true),
            @Param(name = "title", description = "标题"),
            @Param(name = "description", description = "详细描述"),
            @Param(name = "picUrl", description = "图片链接"),
            @Param(name = "url", description = "跳转链接"),
            @Param(name = "msgType", description = "类型"),
            @Param(name = "sort", description = "排序，数字越大越靠前", dataType = "Short")
    })
    @Before(Tx.class)
    @RequiresPermissions("weixin/replyInfo:add")
    public void add() {
        ReplyInfo replyInfo = getBean(ReplyInfo.class, "", true);
        replyInfo.save();
        renderJson(replyInfo);
    }

    @ApiOperation(description = "修改回复内容", url = "/edit")
    @Params({
            @Param(name = "platformId", description = "平台id", required = true),
            @Param(name = "id", description = "内容id", required = true),
            @Param(name = "title", description = "标题"),
            @Param(name = "description", description = "详细描述"),
            @Param(name = "picUrl", description = "图片链接"),
            @Param(name = "url", description = "跳转链接"),
            @Param(name = "msgType", description = "类型"),
            @Param(name = "sort", description = "排序，数字越大越靠前", dataType = "Short")
    })
    @Before(Tx.class)
    @RequiresPermissions("weixin/replyInfo:edit")
    public void edit() {
        ReplyInfo replyInfo = getBean(ReplyInfo.class, "", true);
        replyInfo.update();
        renderJson(replyInfo);
    }

    @ApiOperation(description = "删除回复内容", url = "/delete/{replyInfoId}")
    @Params({
            @Param(name = "platformId", description = "平台id", required = true)
    })
    @Before(Tx.class)
    @RequiresPermissions("weixin/replyInfo:delete")
    public void delete() {
        Integer id = getParaToInt();
        new ReplyInfo().setId(id).delete();
        renderSuccess();
    }
}
